package easy;

import java.util.*;

public class Solution_599 {

    public String[] findRestaurant(String[] list1, String[] list2) {
        Set<String> set = new HashSet<>();
        int min = Integer.MAX_VALUE;
        Map<String, Integer> map = new HashMap<>();
        for (int i = 0; i < list1.length; i++) {
            map.put(list1[i], i);
        }
        for (int i = 0; i < list2.length; i++) {
            Integer index = map.get(list2[i]);
            if (index != null) {
                int v = index + i;
                if (v < min) {
                    min = v;
                    set.clear();
                    set.add(list2[i]);
                } else if (v == min) {
                    set.add(list2[i]);
                }
            }
        }
        return set.toArray(new String[0]);
    }

    public static void main(String[] args) {
        Solution_599 model = new Solution_599();
        String[] restaurant = model.findRestaurant(new String[]{"Shogun", "Tapioca Express", "Burger King", "KFC"}, new String[]{"Piatti", "The Grill at Torrey Pines", "Hungry Hunter Steakhouse", "Shogun"});
        System.out.println();
    }
}
